/**
 *
 */
package com.evil.DAO;

import com.evil.Bean.AnswerBean;
import com.evil.DB_Utils.DB_Utils;
import com.evil.DB_Utils.QueryCallBack;
import com.evil.DB_Utils.UpdateSqlUrl;
import com.evil.Data.Data;
import com.evil.Utils.PropertiesUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/******************************************
 * 工程名：EdaSupport<br>
 * 包名：com.evil.DAO<br>
 * 时间：2016年8月24日 上午10:03:44<br>
 * 作者：白雨濃 evil<br>
 * 邮箱：evilbai@foxmail.com<br>
 * 内容：<br>
 ******************************************/
public class AnswerDAO
{
    public static final int MORE_MAXTIMES = -1;
    public static final int TIMES_NOMORE = 0;

    public ArrayList<AnswerBean> findByitemId(String itemId)
    {
        final String url = "SELECT\n" + "answer_item.answer_id,\n"
                + "answer_item.title,\n" + "answer_item.url,\n"
                + "answer_item.img,\n" + "answer_item.yes,\n"
                + "answer_item.`no`,\n" + "answer_item.type,\n"
                + "answer_item.flow\n" + "FROM\n" + "answer_item\n" + "WHERE\n"
                + "answer_item.answer_id LIKE ?";

        ArrayList<AnswerBean> list = new ArrayList<AnswerBean>();

        DB_Utils.query(url).add(itemId + "%").build()
                .getAll(new QueryCallBack()
                {
                    @Override
                    public void onCallBack(Next next)
                    {
                        AnswerBean bean = new AnswerBean();
                        bean.setId(next.getString(1));
                        bean.setTilte(next.getString(2));
                        bean.setUrl(next.getString(3));
                        bean.setImg(next.getString(4));
                        bean.setYes(next.getString(5));
                        bean.setNo(new Gson().fromJson(next.getString(6),
                                new TypeToken<String[]>()
                                {
                                }.getType()));
                        bean.setType(next.getInt(7));
                        bean.setFlow(next.getInt(8));
                        list.add(bean);
                    }
                }).close();


        return list;
    }

    private int isOverTimesFlag;

    /**
     * 查询是否超过每天答题的最大次数（查询分为进入时查询，和答题完成查询，进入查询不需要times++,答题完成才需要times++）
     *
     * @param phone  手机
     * @param update 是否times++
     * @return
     */
    public int isOverTimes(String phone, boolean update)
    {
        isOverTimesFlag = 0;
        final String query_url = "SELECT\n" +
                "answer_item_times_oneday.phone,\n" +
                "answer_item_times_oneday.date,\n" +
                "answer_item_times_oneday.times\n" +
                "FROM\n" +
                "answer_item_times_oneday\n" +
                "WHERE\n" +
                "answer_item_times_oneday.phone = ?";

        final String insert_url = "INSERT INTO `edasupport`.`answer_item_times_oneday`"
                + " (`phone`, `date`, `times`)"
                + " VALUES (?,?,?);\n";

        String update_url = "UPDATE `edasupport`.`answer_item_times_oneday` SET`date`=?, `times`=? WHERE (`phone`=?);";

        DB_Utils.query(query_url).add(phone).build().get(next ->
        {
            if (next.next())
            {
                int times = next.getInt(3);
                /*判断是否为当天的日期，如果是，则times++，如果不是，更新为当天事件，times=0*/
                if (new SimpleDateFormat("yyyy-MM-dd").format(next.getDate(2))
                        .equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date())))
                {
                    PropertiesUtils utils = new PropertiesUtils();
                    if (times >= utils.getInt("MAX_OVERANSWER_TIMES_ONEDAY"))
                    {
                        isOverTimesFlag = MORE_MAXTIMES;
                    }
                    else if (update)
                    {
                        times++;
                        String re = DB_Utils
                                .update(UpdateSqlUrl
                                        .getUrl(utils.get("DB_Name")
                                                , "answer_item_times_oneday"
                                                , "times", "phone"))
                                .add(times)
                                .add(phone)
                                .build()
                                .close();

                        if (re.equals("1"))
                            isOverTimesFlag = TIMES_NOMORE;
                        else
                            isOverTimesFlag = MORE_MAXTIMES;
                    }
                }
                else
                {
                    String re = DB_Utils.update(update_url).add(new Date()).add(0).add(phone).build().close();
                }
            }
            else
            {
                DB_Utils.insert(insert_url).add(phone).add(new Date()).add(0).build().close();
            }
        }).close();

        return isOverTimesFlag;
    }

    /**
     * 更新答题次数
     */
    private void updateOverTimes()
    {

    }


}
